package vip.ola.pay.channel.abc;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.ola.core.common.util.*;
import vip.ola.core.entity.PayOrder;
import vip.ola.core.entity.PayPassageAccount;
import vip.ola.pay.ctrl.common.BaseController;
import vip.ola.pay.mq.BaseNotify4MchPay;
import vip.ola.pay.service.RpcCommonService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static vip.ola.core.common.constant.CacheKey.ORDER_PAY;
import static vip.ola.core.common.constant.CodeConstant.OPERATE_PHASE_PAY;
import static vip.ola.core.common.constant.Constant.UTF8;
import static vip.ola.core.common.constant.PayConstant.*;
import static vip.ola.core.common.constant.PayEnum.ERR_0010;
import static vip.ola.core.common.constant.PayEnum.ERR_0121;

@Controller
@RequestMapping("/api/abc")
public class AbcPayController extends BaseController {

    private static final MyLog _log = MyLog.getLog(AbcPayController.class);

    @Autowired
    private RpcCommonService rpcCommonService;
    @Autowired
    public BaseNotify4MchPay baseNotify4MchPay;

    /**
     * 订单查询
     *
     * @param request
     * @return
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("notify")
    @ResponseBody
    public String notify(@RequestBody JSONObject jsonObject) {
        _log.info("接收Abc回调：" + jsonObject.toJSONString());
        String mchOrderNo = jsonObject.getString("out_trade_no");
        PayOrder payOrder = rpcCommonService.rpcPayOrderService.findByMchOrderNo(mchOrderNo);
        if(payOrder == null){
            return "order not exist!";
        }
        if (payOrder.getStatus() == PAY_STATUS_SUCCESS || payOrder.getStatus() == PAY_STATUS_COMPLETE) {
            return "success";
        }
        PayPassageAccount payPassageAccount = rpcCommonService.rpcPayPassageAccountService.findById(payOrder.getPassageAccountId());
        if(payPassageAccount == null || StringUtils.isBlank(payPassageAccount.getParam())){
            return "fail";
        }
        if ("success".equals(jsonObject.getString("status"))) {
            String retSign = jsonObject.getString("sign");
            jsonObject.remove("sign");
            String sign = AbcPaymentService.getSign(jsonObject, payPassageAccount.getPrivateKey()).toUpperCase();
            if (retSign.equals(sign)) {
                int result = rpcCommonService.rpcPayOrderService.updateStatus4Success(payOrder.getPayOrderId());
                _log.info("更新订单状态为支付成功: result={}", result);
                if(result == 1){
                    // 通知业务系统
                    baseNotify4MchPay.doNotify(payOrder, true);
                    return "success";
                }
            }
        }
        return "fail";
    }


}
